<?php

namespace App\Admin\Controllers;

use App\Models\CouponType;
use App\Admin\Repositories\Good;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\Goods;

class GoodController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Good('type'), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('type.type_name','分类名称');
            $grid->column('goods_name');
            $grid->column('original_price','原价');
            $grid->column('goods_price','实价');
            //$grid->column('freight_price','');
            $grid->column('goods_cove')->image('',50,50);
            //$grid->column('goods_info');
            $grid->column('sold_total');
            $grid->column('stock_id');
            $grid->column('sort');
            $grid->column('stock');
            $grid->column('is_opend','状态')->using((new Goods())->isOpen());
            //$grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('type_id','分类id')->select(CouponType::pluck('type_name','id')->toArray());
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Good(), function (Show $show) {
            $show->field('id');
            $show->field('type_id');
            $show->field('goods_name');
            $show->field('original_price');
            $show->field('goods_price');
            $show->field('freight_price');
            $show->field('goods_cove')->image();
            $show->field('goods_info');
            $show->field('sold_total');
            $show->field('stock_id');
            $show->field('sort');
            $show->field('stock');
            $show->field('is_opend');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Good(), function (Form $form) {
            $form->display('id');
            $form->select('type_id','分类id')->options(CouponType::pluck('type_name','id'));
            $form->text('goods_name');
            $form->currency('original_price');
            $form->currency('goods_price','售价');
            $form->currency('freight_price','运费');
            $form->image('goods_cove');
            $form->editor('goods_info');
            $form->text('sold_total');
            $form->text('stock_id');
            $form->text('sort');
            $form->text('stock');
            $form->text('is_opend');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
